; $Id$
;
;+
; NAME:
;   FIXAPOFITS
;
; PURPOSE:
;   This procedure allows the user to select new files to be added
;   to a list to then be processed and fix the RA--TAN keyword problem
;   with the fits files generated at APO on the November 2004 run only.
;
; CALLING SEQUENCE:
;   FIXAPOFITS
;
; OPTIONAL INPUTS:
;    NONE
;
; MODIFICATION HISTORY:
;    Written by:   SBeland CU-Boulder/CASA, September 2004
;-

;----------------------------------------------------------------------------
PRO FIXAPOFITS_EVENT, sEvent

    WIDGET_CONTROL, sEvent.id, GET_UVALUE=uval

    ; Handle KILL requests.
    IF TAG_NAMES(sEvent, /STRUCTURE_NAME) EQ 'WIDGET_KILL_REQUEST' THEN BEGIN
       ; Destroy the objects.
       WIDGET_CONTROL, sEvent.top, /DESTROY
       RETURN
    ENDIF

    ; Get the state structure stored in the user value of the window
    WIDGET_CONTROL, GET_UVALUE=state, sEvent.top, /NO_COPY

    ; Handle other events.
    CASE uval OF
        'SELECT' : BEGIN
            ; bring up the file browser and add to list
            fileName = DIALOG_PICKFILE(GROUP=state.wBase, /MULTIPLE_FILES, /MUST_EXIST, $
                       PATH=state.initPath, GET_PATH=newpath, FILTER="*.fits")
            IF newpath NE state.initPath AND newpath NE '' THEN state.initPath = newpath

            ; If the user didn't cancel the standard file dialog
            IF STRLEN(fileName[0]) GT 0 THEN BEGIN

               ; first make sure that it is not already in the list
               IF (*state.listarr)[0] EQ "" THEN BEGIN
                  ; list is empty
                  PTR_FREE, state.listarr
                  state.nlist = N_ELEMENTS(fileName)
                  state.listarr = PTR_NEW(fileName, /NO_COPY)
               ENDIF ELSE BEGIN
                  FOR i=0, N_ELEMENTS(fileName)-1 DO BEGIN
                     pos = WHERE(fileName[i] EQ (*state.listarr)[0:state.nlist-1])
                     IF pos[0] EQ -1 THEN BEGIN
                        *state.listarr = [*state.listarr, fileName[i]]
                        state.nlist = state.nlist + 1
                     ENDIF
                  ENDFOR
               ENDELSE
               WIDGET_CONTROL, state.wList, SET_VALUE=(*state.listarr)
               WIDGET_CONTROL, state.wProcessButton, SENSITIVE=1
               WIDGET_CONTROL, state.wClearButton, SENSITIVE=1
               WIDGET_CONTROL, state.wListLabel, $
                  SET_VALUE='Files to Process:  '+STRTRIM(STRING(state.nlist),2)
            ENDIF
        END

        'REMOVE' : BEGIN
            ; remove selected files from the list
            pos = WIDGET_INFO(state.wList, /LIST_SELECT)
            IF pos[0] NE -1 THEN BEGIN
               (*state.listarr)[pos] = ""
               state.nlist = state.nlist - N_ELEMENTS(pos)
               IF state.nlist EQ 0 THEN BEGIN
                  pos=0
                  PTR_FREE, state.listarr
                  state.listarr = PTR_NEW("")
                  WIDGET_CONTROL, state.wRemoveButton, SENSITIVE=0
                  WIDGET_CONTROL, state.wClearButton, SENSITIVE=0
                  WIDGET_CONTROL, state.wProcessButton, SENSITIVE=0
               ENDIF ELSE BEGIN
                  newlist = (*state.listarr)[WHERE(*state.listarr ne "")]
                  PTR_FREE, state.listarr
                  state.listarr = PTR_NEW(newlist, /NO_COPY)
               ENDELSE
               WIDGET_CONTROL, state.wList, SET_VALUE=(*state.listarr)
               WIDGET_CONTROL, state.wRemoveButton, SENSITIVE=0
               WIDGET_CONTROL, state.wListLabel, $
                  SET_VALUE='Files to Process:  '+STRTRIM(STRING(state.nlist),2)
            ENDIF

          END

        'CLEAR' : BEGIN
            ; clear all the files in the list
            PTR_FREE, state.listarr
            state.listarr = PTR_NEW("")
            state.nlist = 0
            WIDGET_CONTROL, state.wList, SET_VALUE=['']
            WIDGET_CONTROL, state.wRemoveButton, SENSITIVE=0
            WIDGET_CONTROL, state.wClearButton, SENSITIVE=0
            WIDGET_CONTROL, state.wProcessButton, SENSITIVE=0
            WIDGET_CONTROL, state.wListLabel, $
               SET_VALUE='Files to Process:  '+STRTRIM(STRING(state.nlist),2)
          END

        'PROCESS' : BEGIN
             ; disable the buttons while processing
             WIDGET_CONTROL, state.wRemoveButton, SENSITIVE=0
             WIDGET_CONTROL, state.wClearButton, SENSITIVE=0
             WIDGET_CONTROL, state.wProcessButton, SENSITIVE=0
             WIDGET_CONTROL, state.wCancelButton, SENSITIVE=0
             WIDGET_CONTROL, state.wSelectButton, SENSITIVE=0

             ; initialize a progress meter
             p_meter = progress_meter(Text='Fixing FITS files', color=255)

             ; loop through every file and send to sdi2fits
             i=0
             n=state.nlist

             WHILE state.nlist NE 0 DO BEGIN
                ; check if cancel button was pressed
                IF (*p_meter).cancelFlag THEN BEGIN
                   progress_destroy, p_meter
                   WIDGET_CONTROL, state.wCancelButton, SENSITIVE=1
                   WIDGET_CONTROL, state.wSelectButton, SENSITIVE=1
                   WIDGET_CONTROL, state.wRemoveButton, SENSITIVE=1
                   WIDGET_CONTROL, state.wClearButton, SENSITIVE=1
                   IF WIDGET_INFO(sEvent.top, /VALID_ID) THEN $
                      WIDGET_CONTROL, SET_UVALUE = state, sEvent.top, /NO_COPY
                   RETURN
                ENDIF

                print,'('+strtrim(string(i+1),2)+'/'+strtrim(string(n),2)+') Processing '+$
                      (*state.listarr)[0]

                ; start processing the file (will be overwrtten)
                img = mrdfits((*state.listarr)[0], 0, header, /silent, /unsigned)
                sxdelpar, header, 'RA---TAN'
                sxdelpar, header, 'DEC--TAN'
                value = sxpar(header, 'CTYPE1',count=count)
                comment='WCS projection'
                if count > 0 then begin
                    sxdelpar, header, 'CTYPE1'
                endif
                sxaddpar, header, 'CTYPE1', 'RA---TAN', comment, before='CTYPE2'
                value = sxpar(header, 'CTYPE2',count=count)
                if count > 0 then begin
                    sxdelpar, header, 'CTYPE2'
                endif
                sxaddpar, header, 'CTYPE2', 'DEC--TAN', comment, after='CTYPE1'
                mwrfits, img, (*state.listarr)[0], header, /create, /no_comment

                ; set the percentage done
                i=i+1
                progress_update, p_meter, FLOAT(i)*100.0 / FLOAT(n)

                ; remove the entry from the list
                (*state.listarr)[0] = ""
                state.nlist = state.nlist - 1
                IF state.nlist EQ 0 THEN BEGIN
                   PTR_FREE, state.listarr
                   state.listarr = PTR_NEW('')
                   WIDGET_CONTROL, state.wRemoveButton, SENSITIVE=0
                   WIDGET_CONTROL, state.wClearButton, SENSITIVE=0
                   WIDGET_CONTROL, state.wCancelButton, SENSITIVE=1
                   WIDGET_CONTROL, state.wSelectButton, SENSITIVE=1
                   WIDGET_CONTROL, state.wProcessButton, SENSITIVE=0
                   WIDGET_CONTROL, state.wList, SET_VALUE=['']
                ENDIF ELSE BEGIN
                   newlist = (*state.listarr)[WHERE(*state.listarr NE "")]
                   PTR_FREE, state.listarr
                   state.listarr = PTR_NEW(newlist, /NO_COPY)
                   WIDGET_CONTROL, state.wList, SET_VALUE=(*state.listarr)
                ENDELSE

             ENDWHILE

             ; get rid of meter
             progress_destroy, p_meter

             ; re-enable the buttons
             IF WIDGET_INFO(state.wList, /LIST_SELECT) EQ -1 THEN BEGIN
                WIDGET_CONTROL, state.wRemoveButton, SENSITIVE=0
                WIDGET_CONTROL, state.wClearButton, SENSITIVE=0
             ENDIF ELSE BEGIN
                WIDGET_CONTROL, state.wRemoveButton, SENSITIVE=1
                WIDGET_CONTROL, state.wClearButton, SENSITIVE=1
             ENDELSE
             WIDGET_CONTROL, state.wListLabel, $
                SET_VALUE='Files to Process:  '+STRTRIM(STRING(state.nlist),2)

          END

        'CANCEL' : BEGIN
            ; quit without changing the dB
            WIDGET_CONTROL, sEvent.top, /DESTROY
          END

        'LIST' : BEGIN
            ; a file was selected from the list
            pos = WIDGET_INFO(state.wList, /LIST_SELECT)
            IF pos[0] NE -1 THEN BEGIN
               WIDGET_CONTROL, state.wRemoveButton, SENSITIVE=1
            ENDIF
        END

        ELSE:
            ; ignore all other events
    ENDCASE

    ; Reset the windows user value to the updated state structure
    IF WIDGET_INFO(sEvent.top, /VALID_ID) THEN $
       WIDGET_CONTROL, SET_UVALUE = state, sEvent.top, /NO_COPY

END

;----------------------------------------------------------------------------
PRO FIXAPOFITS

    ; Create the widgets.
    xsize = 130

    title = 'FIXAPOFITS  '
    wBase = WIDGET_BASE(/ROW, TITLE=title, /TLB_KILL_REQUEST_EVENTS, TLB_FRAME_ATTR=1)

    wGuiBase      = WIDGET_BASE(wBase, ROW=3)
    wListBase     = WIDGET_BASE(wGuiBase, /COLUMN)
    wListLabel    = WIDGET_LABEL(wListBase, VALUE='Files to Process:  0', $
                    /ALIGN_LEFT, /DYNAMIC_RESIZE)
    wList         = WIDGET_LIST(wListBase, /MULTIPLE, XSIZE=30, YSIZE=10, UVALUE='LIST')

    wButtons      = WIDGET_BASE(wBase,/COLUMN, SPACE=60)
    wTopButtons   = WIDGET_BASE(wButtons,/COLUMN, SPACE=10)
    wSelectButton = WIDGET_BUTTON(wTopButtons, XSIZE=xsize, VALUE='Select', UVALUE='SELECT')
    wRemoveButton = WIDGET_BUTTON(wTopButtons, XSIZE=xsize, VALUE='Remove', UVALUE='REMOVE')
    wClearButton  = WIDGET_BUTTON(wTopButtons, XSIZE=xsize, VALUE='Clear', UVALUE='CLEAR')

    wBottomButtons= WIDGET_BASE(wButtons,/COLUMN, /ALIGN_BOTTOM, SPACE=5)
    wProcessButton= WIDGET_BUTTON(wBottomButtons, XSIZE=xsize, VALUE='Process', UVALUE='PROCESS')
    wCancelButton = WIDGET_BUTTON(wBottomButtons, XSIZE=xsize, VALUE='Cancel', UVALUE='CANCEL')

   state = { $
             wBase: wBase, $
             wList: wList, $
             wListLabel: wListLabel, $
             wSelectButton: wSelectButton, $
             wRemoveButton: wRemoveButton, $
             wClearButton: wClearButton, $
             wProcessButton: wProcessButton, $
             wCancelButton: wCancelButton, $
             listarr: PTR_NEW(""), $
             nlist: 0, $
             initPath: "./" $
           }

    ; set the initial browsing path to the default raw dir set by environment

    ; Save the state structure in the window's user value
    WIDGET_CONTROL, wBase, SET_UVALUE=state

    WIDGET_CONTROL, wRemoveButton, SENSITIVE=0
    WIDGET_CONTROL, wClearButton, SENSITIVE=0
    WIDGET_CONTROL, wProcessButton, SENSITIVE=0

    WIDGET_CONTROL, wBase, /REALIZE

    XMANAGER, 'FIXAPOFITS', wBase, /NO_BLOCK

END
